package com.zzuli.yxy.task;

import com.zzuli.yxy.entity.Song;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.zzuli.yxy.task.springDataPipeline.SongNamePipeline;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import us.codecraft.webmagic.selector.Selectable;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 目标地址：https://music.163.com/discover/toplist?id=3778678
 * 获取热歌榜歌曲
 * 获取网易云歌曲id 、 歌名
 * @auther pony
 * @create 2023-04-04 14:15
 */
@Component
public class SongNameTask implements PageProcessor {
    // 热歌榜 get
    private static String hotSheetUrl = "https://music.163.com/discover/toplist?id=3778678";

    // 歌曲基本信息 歌手，所属专辑 get
    private static String songBasicUrl = "https://music.163.com/song?id=";

    private List<Song> songList = new LinkedList<>();
    @Override
    public void process(Page page) {
        //        // 存放 song 所有信息
//        List<Song> songList = new LinkedList<>();
        //1、获取歌曲 id
        List<Selectable> songsList = page.getHtml().css("div#song-list-pre-cache ul.f-hide li a").nodes();

        // 如果 songsList 长度不为 0，说明现在爬 热歌榜页面
        if (songsList.size()!=0) {

            for (Selectable selectable : songsList) {
                Song song = new Song();
                // 获取歌曲名
                // System.out.println("selectable)"+selectable.css("a","text"));
                String songName = selectable.css("a", "text").get();
                song.setSongName(songName);

                //获取url地址 https://music.163.com/song?id=2026224214
                String songInfoUrl = selectable.links().toString();
                //System.out.println("歌曲地址:" + songInfoUrl);
                //截取 2026224214
                String songId = songInfoUrl.substring(songInfoUrl.lastIndexOf("=") + 1);
                System.out.println("歌曲id:" + songId);
                song.setSongId(Integer.valueOf(songId));
                songList.add(song);

                //2、添加到任务队列，继续获取歌曲基本信息
//                page.addTargetRequest(songBasicUrl+songId);
            }
//            //歌曲歌词
//            for (Song song1: songList) {
//                System.out.println("song1:"+song1);
//                page.addTargetRequest(songIrcPre+song1.getSongWyyId()+songIrcSuf);
//            }
        }

        page.putField("songInfo", songList);
    }

    private Site site = Site.me();
    @Override
    public Site getSite(){
        return site;
    }

    @Autowired
    private SongNamePipeline songNamePipeline;
    // 获取热歌榜所有歌曲的 id
    // 每周一 凌晨1 点，执行一遍更新数据
    // @Scheduled(cron = "0 0 1 ? * 1")
//    @Scheduled(initialDelay = 1000, fixedRate=1000*60*60*24*7)
    public void process(){
        //1、将目标 url(hotSheetUrl) 封装到一个 Request 中
        Request request = new Request(hotSheetUrl);
        //2、设置请求头信息，请求方法，编码格式，浏览器用户信息（用于针对一些页面的反扒）
        Map<String, Object> map = new HashMap();
        request.setMethod("GET");
        request.setRequestBody(HttpRequestBody.form(map,"UTF-8"));
        request.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36");
        Spider.create(new SongNameTask())
                .addRequest(request)
                // 使用布隆过滤器进行去重， 10000 大概要放的数据，数据越大越慢
                // 使用前先引一个新版本的依赖
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(10000)))
                .thread(10)
                .addPipeline(this.songNamePipeline)
                .run();
    }
}
